package com.sfdcj.se.meta.proc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sfdcj.se.SfdcException;
import com.sfdcj.se.meta.Outcome;
import com.sfdcj.se.meta.field.FieldTypeBuilder;
import com.sfdcj.se.util.Utils;

public class MetadataXMLGenerateProcessor implements Processor {

	static public String LABEL = "PARAM_LABEL";
	static public String TYPE = "PARAM_TYPE";
	static public String FULLNAME = "PARAM_FULLNAME";
	static public String ATTRS = "PARAM_ATTRS";

	static private FieldTypeBuilder builder = new FieldTypeBuilder();

	static String NEXTPAGE = "./meta/fieldxml.jsp";

	public Outcome execute(HttpServletRequest req, HttpServletResponse res)
			throws SfdcException {

		StringBuffer buf = new StringBuffer();

		List<String> typeKeys = Utils.getParamsWithKeywords(req, TYPE);
		for (String typeKey : typeKeys) {
			String index = typeKey.substring(TYPE.length(), typeKey.length());
			String label = Utils.getParameter(req, LABEL + index);
			String fullname = Utils.getParameter(req, FULLNAME + index);
			String attrs = Utils.getParameter(req, ATTRS + index);
			String type = Utils.getParameter(req, typeKey);
			buf.append(builder.create("", type, label, fullname, attrs).toXML());
		}

		Outcome outcome = new Outcome();
		outcome.returnJsp = NEXTPAGE;
		outcome.obj = buf.toString();
		return outcome;
	}
}
